package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection conn;
	DatabaseTest dt;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField serial;
	private JTextField code;
	private JTextField ea;
	private JButton button;
	private JButton button_1;
	private JLabel status;
	private JButton btnNewButton;

	
	public Delete(Connection c, DatabaseTest dt) {
		super("삭제",true,true,true,false);
		setBounds(50, 350, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getButton());
		getContentPane().add(getButton_1());
		getContentPane().add(getStatus());
		getContentPane().add(getBtnNewButton());
		conn =c;
		this.dt=dt;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("serial");
			label.setBounds(39, 25, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("code");
			label_1.setBounds(39, 77, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("ea");
			label_2.setBounds(39, 133, 57, 15);
		}
		return label_2;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(129, 22, 116, 21);
		}
		return serial;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(129, 74, 116, 21);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(129, 130, 116, 21);
		}
		return ea;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("검색");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					int s =Integer.parseInt(serial.getText());
					
					String sql="select*from ptbl where serial = ?" ;
					
					try{
						
						PreparedStatement pstmt=conn.prepareStatement(sql);
						pstmt.setInt(1, s); 
						//select한 여러개의 값들을...
						
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()) {
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
							
						}else{
							status.setText("데이터가 없습니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						rs.close();
						pstmt.close();
						
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			button.setBounds(289, 21, 97, 23);
		}
		return button;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("삭제");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					del();
					
					
				}
			});
			button_1.setBounds(80, 178, 97, 23);
		}
		return button_1;
	}
	public void del(){
		int s = Integer.parseInt(serial.getText()) ;
		
		String sql="delete from ptbl where serial=?";
		
		try{
			
			PreparedStatement pstmt=conn.prepareStatement(sql);
			
			pstmt.setInt(1, s);
			
			pstmt.executeUpdate();
			pstmt.close();
			
			status.setText("데이터가 삭제되었습니다");
			code.setText("");
			ea.setText("");
			serial.setText("");
			serial.requestFocus();
			
			if(dt.select != null)  dt.select.setList();	//조회 목록 갱신
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(39, 211, 335, 29);
		}
		return status;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("전체삭제");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					try{
						PreparedStatement pstmt=conn.prepareStatement("delete from ptbl");	
						pstmt.executeUpdate();
						pstmt.close();
						
						if(dt.select !=null) dt.select.setList();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(289, 178, 97, 23);
		}
		return btnNewButton;
	}
}
